package com;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6030301963604650591L;
	//private boolean menuLoaded;
	private int activeIndex;
	private boolean loggedIn;
	private String page;
	private String username;
	private String password;

	@PostConstruct
	public void init() {
		loggedIn = false;
		activeIndex = Constants.INDEX_LOGIN;
		page = Constants.PAGES[activeIndex];
		System.out.println(activeIndex + " - " + page);
	}

	public void login() {
		if ("murat".equals(username) && "1234".equals(password)) {
			loggedIn = true;
			navigatePage(Constants.INDEX_HOME);
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(Constants.ERROR_TITLE,
							Constants.ERROR_MESSAGE));
		}
	}

	public void logout() {
		init();
	}

	public void menuHome() {
		navigatePage(Constants.INDEX_HOME);
	}

	public void menuStatus() {
		navigatePage(Constants.INDEX_STATUS);
	}

	public void menuProject() {
		navigatePage(Constants.INDEX_PROJECT);
	}

	public void menuComment() {
		navigatePage(Constants.INDEX_COMMENT);
	}

	public void menuUs() {
		navigatePage(Constants.INDEX_US);
		/*if (!menuLoaded) {
			navigatePage(Constants.INDEX_HOME);
			menuLoaded = true;
		}*/
	}

	private void navigatePage(int indexTarget) {
		if (loggedIn) {
			activeIndex = indexTarget;
			page = Constants.PAGES[indexTarget];
		} else {
			activeIndex = Constants.INDEX_HOME;
			page = Constants.PAGES[Constants.INDEX_LOGIN];
		}
		System.out.println(activeIndex + " - " + page);
	}

	public int getActiveIndex() {
		return activeIndex;
	}

	public void setActiveIndex(int activeIndex) {
		this.activeIndex = activeIndex;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
